package com.wu.mapper;

import com.mysql.cj.log.LogFactory;
import com.wu.App;
import com.wu.entity.User;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class wu {

  static Logger logger= LoggerFactory.getLogger(wu.class);

    @Test
    public void test(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
       for (User user : users) {
            System.out.println(user.toString());
        }
    }

    /**
     * 一级缓存是SqlSession级别的缓存，是一直开启的，我们关闭不了它；
     * 本地缓存失效有四种方法，下面只是展现一种
     * 证明一级缓存
     */
    @Test
    public void test1(){
//        logger.info("======================");
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectById(1);
        session.clearCache();//手动清除缓存
        User user1 = mapper.selectById(1);
        System.out.println("user==user1==============="+(user==user1));
        session.close();
        session.close();
    }

    @Test
    public void test2(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectBylikename("帅");
        logger.info(user.toString());
        session.close();
        session.close();
    }
    @Test
    public void test3(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        Date date = new Date();
        user.setName("小美女");
        user.setCreatedate(date);
        for (int i = 4; i < 20; i++) {
            user.setId(i);
            mapper.addUser(user);
        }
        session.commit();
        session.close();
    }
    @Test
    public void test4(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setId(2);
        user.setName("小帅");
        int i = mapper.updateUser(user);
        System.out.println(i);
        session.commit();
        session.close();
    }

    @Test
    public void test5(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.deleteUser(3);
        System.out.println(i);
        session.commit();
        session.close();
    }


    @Test
    public void seletUser(){
        SqlSession session = App.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        int currentPage = 1; //第几页
        int pageSize = 5; //每页显示5个
        Map<String,Integer> map = new HashMap<String,Integer>();
        //因为limit 语言，所以这里要转变  startIndex=(currentPage-1)*pageSize
         map.put("startIndex",(currentPage-1)*pageSize);
         map.put("pageSize",pageSize);
        List<User> users = mapper.selectUserlimit(map);
        for (User user : users) {
            System.out.println(user);
        }
        System.out.println();
        session.commit();
        session.close();
    }

    //分页2  不推荐
    @Test
    public void seletUserRound(){
        SqlSession session = App.getSession();
        int currentPage = 1; //第几页
        int pageSize = 5; //每页显示5个
       RowBounds rowBounds = new RowBounds((currentPage-1)*pageSize,pageSize);
        List<User> users = session.selectList("com.wu.mapper.UserMapper.getUserByRowBounds",null, rowBounds);
        for (User user : users) {
            System.out.println(user);
        }
        session.commit();
        session.close();
    }
}
